<?php

require_once ('application\controllers\basecontroller.php');

class news extends BaseController {
	function __construct() {
		parent::__construct ();
		$this->load->model('newsinfoModel');
		$this->session->set_userdata("navindex",4);
	}
	function index($data=null)
	{
		$this->getnewsinfo(0);
	}
	function _loadContentView()
	{
		if($this->session->userdata("username")==null)
		{
			$this->load->view('loginview');
			return ;
		}
		if (isset($this->baseControllerData[$this->DATATYPE_ERROR]))
		{
			parent::_loadContentView();
			unset($this->baseControllerData[$this->DATATYPE_ERROR]);
		}
		
		else if (!isset($this->baseControllerData["type"])||$this->baseControllerData["type"]==0)
		{
			
			$this->load->view("news/newsinfoview",$this->baseControllerData[$this->DATATYPE_CONTENT]);
		}
		else if ($this->baseControllerData["type"]==1)
		{
			$this->_addNewsInfo();
		}
		else if($this->baseControllerData["type"]==2)
		{
			$this->load->view("news/newsinfodetail",$this->baseControllerData[$this->DATATYPE_CONTENT]);
		}
		else if($this->baseControllerData["type"]==3)
		{
			$this->load->view("news/editnewsinfo",$this->baseControllerData[$this->DATATYPE_CONTENT]);
		}
		else if ($this->baseControllerData["type"]==4)
		{
			$this->load->view("case/caseinfosearch",$this->baseControllerData[$this->DATATYPE_CONTENT]);
		}
		
	}
	function searchinfo()
	{
		$start = intval($_POST["currentPage"]);
		$size = 10;
		$case_name = $_POST["case_name"];
		$case_type = $_POST["case_type"];
		$case_status = $_POST["case_status"];
		$incident_time_begin = $_POST["incident_time_begin"];
		$incident_time_end = $_POST["incident_time_end"];
		$court_date_begin = $_POST["court_date_begin"];
		$court_date_end = $_POST["court_date_end"];
		
		$prosecutor = $_POST["prosecutor"];
		$defendant = $_POST["defendant"];
		$num = $this->caseinfoModel->getcaseinfoByCond($case_name
			,$case_type
			,$case_status
			,$incident_time_begin
			,$incident_time_end
			,$court_date_begin
			,$court_date_end
			,$prosecutor
			,$defendant);
		$list =$this->caseinfoModel->getcaselistByCond($start,$size,$case_name
			,$case_type
			,$case_status
			,$incident_time_begin
			,$incident_time_end
			,$court_date_begin
			,$court_date_end
			,$prosecutor
			,$defendant);
		$data["list"] = $list;
		$data["start"] = $start;
		$data["num"] = $num;
		$data["size"] = $list->num_rows();
		$data["type"] = 0;
		$this->baseControllerData["type"] = 4;
		$this->baseControllerData[$this->DATATYPE_CONTENT]=$data;
		parent::index();
	}
	function getnewsinfo($start)
	{
		$size =10;
		if ($this->session->userdata("username")!= null)
		{
			$this->__listNewsInfo(intval($start),$size);
		}
		else
		{
			redirect(base_url("index.php/user/"));
		}
	}
	function addNewsInfo()
	{
		if ($this->session->userdata("usertype")== 4)
		{
			$this->baseControllerData["type"]=1;
			parent::index();
		}
		else
		{
			redirect(base_url("index.php/user/"));
		}
	}
	function detail($id)
	{
		if ($this->session->userdata("username")!= null)
		{
			$this->baseControllerData["type"]=2;
			$data = $this->newsinfoModel->getnewsinfodetail($id);	         
			$this->baseControllerData[$this->DATATYPE_CONTENT] = $data[0];
			parent::index();
			
		}
	}
	function delete($id)
	{
		if ($this->session->userdata("usertype")== 4)
		{
			$this->baseControllerData["type"]=0;
			$data = $this->newsinfoModel->delete($id);
			$this->getnewsinfo(0);
		}
		else
		{
			$this->showerror("please login as admin");
		}
	}
	function edit($id)
	{
		if ($this->session->userdata("usertype")== 4)
		{
			$this->baseControllerData["type"]=3;
			$data = $this->newsinfoModel->getnewsinfodetail($id);
	
			$this->baseControllerData[$this->DATATYPE_CONTENT] = $data[0];
			parent::index();
		}
			else
		{
			$this->showerror("please login as admin");
		}
	}
	
	function addtomodel()
	{
		if($this->session->userdata("username")==null)
		{
			redirect(base_url("index.php/user/"));
			return ;
		}
		$recommend=isset($_POST["recommend"])?$_POST["recommend"]:0;
		$num = $this->newsinfoModel->addnewsinfo($_POST["new_name"],$recommend,$_FILES["file"],$_POST["new_content"]);
			
		$this->getnewsinfo(0);
	}
	
	function updatetomodel()
	{
		if($this->session->userdata("username")==null)
		{
			redirect(base_url("index.php/user/"));
			return ;
		}
		$recommend=isset($_POST["recommend"])?$_POST["recommend"]:0;
		$num = $this->newsinfoModel->updatenewsinfo($_POST["id"],$_POST["new_name"],$recommend,$_FILES["file"],$_POST["new_content"]);
			
		$this->getnewsinfo(0);

	}
	function search()
	{
		if($this->session->userdata("username")==null)
		{
			redirect(base_url("index.php/user/"));
			return ;
		}
//		$num = $this->caseinfoModel->addcaseinfo($this->session->userdata("userid")
//		,$_POST["incident_time"],$_POST["court_date"]
//			,$_POST["prosecutor"],$_POST["defendant"],$_POST["case_status"]
//			,$_POST["judge_name"],$_POST["case_name"],$_POST["case_content"]);
//			
		$this->baseControllerData["type"] = 4;
		$this->baseControllerData[$this->DATATYPE_CONTENT]= "";
		parent::index();
	}
	function _addNewsInfo()
	{
		$this->load->view("news/addNewsinfo");
	}
	function __listNewsInfo($start,$size)
	{
		//
		$num = $this->newsinfoModel->getnewsinfo();
		//echo "__listcaseinfo is".$num;
		$list =$this->newsinfoModel->getnewslist($start,$size);
		$data["list"] = $list;
		$data["start"] = $start;
		$data["num"] = $num;
		$data["size"] = $list->num_rows();
		$data["type"] = 0;
		$this->baseControllerData[$this->DATATYPE_CONTENT]=$data;
		//$this->load->view("case/caseinfoview",$data);
		parent::index($this->baseControllerData);
	}
	function _loadAsideView()
	{
		$this->load->view("news/newsinfoaside");
	}
}

?>